<?php
	define('IN_PHPBB', true);
	$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
	$phpEx = substr(strrchr(__FILE__, '.'), 1);
	include($phpbb_root_path . 'common.' . $phpEx);
	include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
	// Start session management
	$user->session_begin();
	$auth->acl($user->data);
	$user->setup();
	require($phpbb_root_path ."includes/functions_user.php");
	
	function check_username($username) {
	    if (eregi("[2|3|4|5|6|7|8|9|G|K|V][0-9][0-9][0-9][0-9][0-9][0-9][0-9]", $username))
	    	return true;
	    return false;
	}
	if (!isset($_POST['username']) && !isset($_POST['password']) && !isset($_POST['repassword'])) {
		header("Location: index.php");
	}
	else{
		$username = $_POST['username'];
		$password = $_POST['password'];
		$repassword = $_POST['repassword'];
		$email = $username . "hcmut.edu.vn";

		if ($username == "" || $password == "" || $repassword == "") {
			header("Location: register.php?error=0");
		}
		elseif (!check_username($username)) {
			header("Location: register.php?error=1");
		}
		elseif ($password != $repassword) {
			header("Location: register.php?error=2");
		}
		elseif (validate_username($username) == "USERNAME_TAKEN") {
			header("Location: register.php?error=3");
		}
		else{
			$user_data = array(
			   'username' => $username,
			   'user_password' => md5($password),
			   'user_email' => $email,
			   'group_id' => 2,
    		   'user_timezone' => 7.00,
			   'user_lang' => 'en',
			   'user_type' => 0,
			   'user_regdate' => time(),
			);
			      
			// Register user...
			$user_id = user_add($user_data);
			//header("Location: ucp.php?mode=login");
			header("Location: register.php?result=success");
		}
	}
?>